package com.G_CN06.mvc.service.impl;

import com.G_CN06.commonUtil.Auth.AuthTypeCheck;
import com.G_CN06.commonUtil.commonResultConfig.CommonResult;
import com.G_CN06.mvc.entity.FillQuestion;
import com.G_CN06.mvc.entity.User;
import com.G_CN06.mvc.mapper.FillQuestionMapper;
import com.G_CN06.mvc.service.FillQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Huang
 * @since 2023-02-09
 */
@Service
public class FillQuestionServiceImpl extends ServiceImpl<FillQuestionMapper, FillQuestion> implements FillQuestionService {

    @Resource
    FillQuestionMapper fillQuestionMapper;


    @Override
    public CommonResult saveForTeacher(FillQuestion fillQuestion, HttpSession session) {
        CommonResult result = new CommonResult();
        //从session获取用户信息
        User user = (User) session.getAttribute("LoadingUser");
        if (AuthTypeCheck.isTeacher1(user.getAuth())) {
            fillQuestion.setTeacher(user.getUId());
            fillQuestionMapper.insert(fillQuestion);
            return result.ok().message("插入数据成功");
        }
        return result.error().message("插入失败，您可能无出题权限!");
    }

    @Override
    public CommonResult getConditionalQuestion(String searchString) {
        CommonResult r = new CommonResult();
        System.out.println(searchString);
        QueryWrapper<FillQuestion> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(searchString), "subject", searchString)
                .or()
                .like(StringUtils.isNotEmpty(searchString), "question", searchString);
        return r.ok("list", baseMapper.selectList(wrapper));
    }

    @Override
    public List<FillQuestion> getRandomQuestions(int numOfFillQuestions) {
        return null;
    }
}
